home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 April: Mac OS SDK / Dev.CD Apr 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Samples / SampleCode / QD3D Pascal Demos / START HERE - Box / BoxShell.p < prev   
Encoding:
Text File  |  1996-11-15  |  9.9 KB  |  345 lines  |  [TEXT/CWIE]

  1. {This file was processed by Dan's Source Converter}
  2. {version 1.3 (this version modified by Ingemar Ragnemalm)}
  3.  
  4. {09-15-96 Should be working now}
  5. {Matt Mora fixed some porting bugs}
  6. {mxmora@apple.com}
  7.  
  8. program BoxShell;
  9.  
  10.     uses
  11.         Types, QuickDraw, Events, Windows, Dialogs, Fonts, DiskInit, TextEdit, Traps,{}
  12.         Memory, SegLoad, Scrap, ToolUtils, OSUtils, Menus, Resources, StandardFile,{}
  13.         GestaltEqu, Files, Errors, Devices, QuickDrawText, TextUtils, {}
  14.  
  15. {End of standard head. Add more units to uses as necessary.}
  16.  
  17. { BoxShell.c - QuickDraw 3d routines}
  18. {}
  19. { This is box, the QuickDraw 3D starter program.  Written for the}
  20. { Getting started with QuickDraw 3D Develop article.  This app does not have }
  21. { graceful error handling - it's putpose is to illustrate a very basic QuickDraw }
  22. { 3D program.}
  23. {}
  24. { Nick Thompson - January 6th 1995}
  25. { }
  26. { ©1994-95 Apple computer Inc., All Rights Reserved}
  27. {}
  28.  
  29. { system headers}
  30.         PictUtils, QDOffScreen, 
  31.  
  32. { for QuickDraw 3D}
  33.         QD3D, QD3DMath, QD3DDrawContext, QD3DShader, QD3DTransform, QD3DGroup, QD3DStyle, QD3DView, Box3DSupport;
  34.  
  35.     const
  36.         mApple = 128;
  37.         mFile = 129;
  38.         mEdit = 130;
  39.         mTest = 131;
  40.  
  41.     const
  42.         iAbout = 1;
  43.  
  44.     const
  45.         iNew = 1;
  46.         iOpen = 2;
  47.         iClose = 3;
  48.         iUnused1 = 4;
  49.         iQuit = 5;
  50.  
  51. {-------------------------------------------------------------------------------------------}
  52. {}
  53.     const
  54.         iUsePictPalette = 1;
  55.  
  56. {-------------------------------------------------------------------------------------------}
  57. { constants - defined in SmallShell.c}
  58.     var
  59.         kRGBBlack: RGBColor;
  60.         kRGBWhite: RGBColor;
  61.  
  62. {-------------------------------------------------------------------------------------------}
  63.  
  64.     type
  65.         _documentRecord = record
  66.                 fView: TQ3ViewObject;                    { the view for the scene}
  67.                 fModel: TQ3GroupObject;                { object in the scene being modelled}
  68.                 fInterpolation: TQ3StyleObject;        { interpolation style used when rendering}
  69.                 fBackFacing: TQ3StyleObject;            { whether to draw shapes that face away from the camera}
  70.                 fFillStyle: TQ3StyleObject;            { whether drawn as solid filled object or decomposed to components}
  71.                 fRotation: TQ3Matrix4x4;                { the transform for the model}
  72.             end;
  73.  
  74.     type
  75.         DocumentRec = _documentRecord;
  76.         DocumentPtr = ^DocumentRec;
  77.         DocumentHdl = ^DocumentPtr;
  78.  
  79. {-------------------------------------------------------------------------------------------}
  80. {}
  81.  
  82.     var
  83.         gQuitFlag: Boolean;
  84.         gMainWindow: WindowPtr;
  85.         gDocument: DocumentRec;
  86.         dummyStatus: TQ3Status;
  87.  
  88. {-------------------------------------------------------------------------------------------}
  89. {}
  90.  
  91. {-------------------------------------------------------------------------------------------}
  92.     procedure InitDocumentData (theDocument: DocumentPtr);
  93. {-------------------------------------------------------------------------------------------}
  94.         var
  95.             dummyMatrix: TQ3Matrix4x4Ptr;
  96.     { sets up the 3d data for the scene}
  97.     { Create view for QuickDraw 3D.}
  98.     begin
  99.         theDocument^.fView := MyNewView(gMainWindow);
  100.  
  101.     { the main display group:}
  102.         theDocument^.fModel := MyNewModel;
  103.  
  104.     { the drawing styles:}
  105.         theDocument^.fInterpolation := Q3InterpolationStyle_New(kQ3InterpolationStyleNone);
  106.         theDocument^.fBackFacing := Q3BackfacingStyle_New(kQ3BackfacingStyleBoth);
  107.         theDocument^.fFillStyle := Q3FillStyle_New(kQ3FillStyleFilled);
  108.  
  109.     { set the rotation matrix the identity matrix}
  110.         dummyMatrix := Q3Matrix4x4_SetIdentity(theDocument^.fRotation);
  111.     end;
  112.  
  113. {-------------------------------------------------------------------------------------------}
  114.     procedure DisposeDocumentData (theDocument: DocumentPtr);
  115. {-------------------------------------------------------------------------------------------}
  116.  
  117.     begin
  118.         if theDocument^.fView <> nil then
  119.             begin
  120.                 dummyStatus := Q3Object_Dispose(theDocument^.fView);                { the view for the scene}
  121.             end;
  122.         dummyStatus := Q3Object_Dispose(theDocument^.fModel);                { object in the scene being modelled}
  123.         dummyStatus := Q3Object_Dispose(theDocument^.fInterpolation);        { interpolation style used when rendering}
  124.         dummyStatus := Q3Object_Dispose(theDocument^.fBackFacing);        { whether to draw shapes that face away from the camera}
  125.         dummyStatus := Q3Object_Dispose(theDocument^.fFillStyle);            { whether drawn as solid filled object or decomposed to components}
  126.  
  127.     end;
  128.  
  129. {-------------------------------------------------------------------------------------------}
  130.     function DocumentDraw3DData (theDocument: DocumentPtr): TQ3Status;
  131. {-------------------------------------------------------------------------------------------}
  132.     begin
  133.         if theDocument^.fView <> nil then
  134.             begin
  135.  
  136.                 dummyStatus := Q3View_StartRendering(theDocument^.fView);
  137.                 repeat
  138.                     dummyStatus := Q3Style_Submit(theDocument^.fInterpolation, theDocument^.fView);
  139.                     dummyStatus := Q3Style_Submit(theDocument^.fBackFacing, theDocument^.fView);
  140.                     dummyStatus := Q3Style_Submit(theDocument^.fFillStyle, theDocument^.fView);
  141.                     dummyStatus := Q3MatrixTransform_Submit(theDocument^.fRotation, theDocument^.fView);
  142.                     dummyStatus := Q3DisplayGroup_Submit(theDocument^.fModel, theDocument^.fView);
  143.                 until not (Q3View_EndRendering(theDocument^.fView) = kQ3ViewStatusRetraverse);
  144.                 DocumentDraw3DData := kQ3Success;
  145.             end
  146.         else
  147.             begin
  148.                 DocumentDraw3DData := kQ3Failure;
  149.             end;
  150.  
  151.     end;
  152.  
  153.  
  154. {-------------------------------------------------------------------------------------------}
  155.     function HiWrd (aLong: LongInt): Integer;
  156. {-------------------------------------------------------------------------------------------}
  157.     begin
  158.         HiWrd := Point(aLong).v;
  159.     end;
  160.  
  161. {-------------------------------------------------------------------------------------------}
  162.     function LoWrd (aLong: LongInt): Integer;
  163. {-------------------------------------------------------------------------------------------}
  164.     begin
  165.         LoWrd := Point(aLong).h;
  166.     end;
  167.  
  168. {-------------------------------------------------------------------------------------------}
  169.     procedure InitToolbox;
  170. {-------------------------------------------------------------------------------------------}
  171.     begin
  172.         MaxApplZone;
  173.         MoreMasters;
  174.         MoreMasters;
  175.         MoreMasters;
  176.  
  177.         InitGraf(@qd.thePort);
  178.         InitFonts;
  179.         InitWindows;
  180.         InitCursor;
  181.  
  182.         FlushEvents(everyEvent, 0);
  183.     { initialize application globals}
  184.  
  185.         gQuitFlag := false;
  186.  
  187.     end;
  188.  
  189. {-------------------------------------------------------------------------------------------}
  190.     procedure HandleKeyPress (var event: EventRecord);
  191. {-------------------------------------------------------------------------------------------}
  192.     begin
  193.     end;
  194.  
  195. {-------------------------------------------------------------------------------------------}
  196. procedure RotateCube(window:WindowRef);
  197. {-------------------------------------------------------------------------------------------}
  198.         var
  199.             tmp: TQ3Matrix4x4;
  200.             theRect: Rect;
  201.             dummyMatrix: TQ3Matrix4x4Ptr;
  202. begin
  203.     theRect := gMainWindow^.portRect;
  204.         SetPort(gMainWindow);
  205.         dummyMatrix := Q3Matrix4x4_SetRotate_XYZ(tmp, 0.1, 0.12, 0.08);
  206.         dummyMatrix := Q3Matrix4x4_Multiply(gDocument.fRotation, tmp, gDocument.fRotation);
  207.  
  208.         InvalRect(theRect);
  209. end;
  210.  
  211. {-------------------------------------------------------------------------------------------}
  212.     procedure MainEventLoop;
  213. {-------------------------------------------------------------------------------------------}
  214.  
  215.         var
  216.             event: EventRecord;
  217.             window: WindowPtr;
  218.             thePart: Integer;
  219.             screenRect, updateRect: Rect;
  220.             aPoint: Point;
  221.             savedPort: CGrafPtr;
  222.         
  223.     begin
  224.         
  225.         aPoint.h := 100;
  226.         aPoint.v := 100;
  227.  
  228.         while not gQuitFlag do
  229.             begin
  230.             
  231.                 if WaitNextEvent(everyEvent, event, 0, nil) then
  232.                     begin
  233.                         case (event.what) of
  234.                             mouseDown: 
  235.                                 begin
  236.                                     thePart := FindWindow(event.where, window);
  237.  
  238.                                     case thePart of
  239.                                         inMenuBar: 
  240.                                             ;
  241.  
  242.                                         inDrag: 
  243.                                             begin
  244.                                                 screenRect := GetGrayRgn^^.rgnBBox;
  245.                                                 DragWindow(window, event.where, screenRect);
  246.                                             end;
  247.  
  248.                                         inContent: 
  249.                                             if (window <> FrontWindow) then
  250.                                                 SelectWindow(window);
  251.  
  252.                                         inGoAway: 
  253.                                             if (TrackGoAway(window, event.where)) then
  254.                                                 begin
  255.                                                     DisposeWindow(window);
  256.                                                     gQuitFlag := true;
  257.  
  258.                                                 end;
  259.  
  260.                                         otherwise
  261.  
  262.                                     end; {case}
  263.                                 end; {mouseDown}
  264.  
  265.  
  266.                             updateEvt: 
  267.                                 begin
  268.                                     window := WindowPtr(event.message);
  269.                                     updateRect := window^.visRgn^^.rgnBBox;
  270.                                     SetPort(window);
  271.                                     BeginUpdate(window);
  272.                                     dummyStatus := DocumentDraw3DData(@gDocument);
  273.                                     EndUpdate(window);
  274.                                 end;
  275.  
  276.                             keyDown, autoKey: 
  277.                                 HandleKeyPress(event);
  278.  
  279.                             diskEvt: 
  280.                                 if (HiWrd(event.message) <> noErr) then
  281.                                     if DIBadMount(aPoint, event.message) <> noErr then
  282.                                         ;
  283.  
  284.                             osEvt, activateEvt: 
  285.                                 ;
  286.  
  287.  
  288.                         end;
  289.                         RotateCube(window);
  290.                     end
  291.                 else
  292.             { we received a null event, rotate the cube}
  293.  
  294.                     begin
  295.                         RotateCube(window);
  296.                     end;
  297.                 
  298.             end;
  299.     end;
  300.  
  301.  
  302.  
  303. {-------------------------------------------------------------------------------------------}
  304. { main()}
  305. {-------------------------------------------------------------------------------------------}
  306. { entry point for the application, initialize the toolbox, initialize QuickDraw 3D}
  307. { and enter the main event loop.  On exit from the main event loop, we want to call}
  308. { the QuickDraw 3D exit function to clean up QuickDraw 3d.}
  309.  
  310. {main}
  311.  
  312.     var
  313.         myStatus: TQ3Status;
  314.         rBounds: Rect;
  315.     const
  316.         title = 'Spinning Box (Pascal)';
  317.  
  318. begin
  319.     InitToolbox;
  320.  
  321.     SetRect(rBounds, 50, 50, 450, 450);
  322.  
  323.     {    Initialize QuickDraw 3D, open a connection to the QuickDraw 3D library}
  324.     myStatus := Q3Initialize;
  325.  
  326.     if (myStatus = kQ3Failure) then
  327.         DebugStr('ErInitialize returned failure.');
  328.  
  329.     { set up our globals}
  330.     gQuitFlag := false;
  331.     gMainWindow := NewCWindow(nil, rBounds, title, true, noGrowDocProc, WindowPtr(-1), true, 0);
  332.  
  333.     InitDocumentData(@gDocument);
  334.  
  335.     MainEventLoop;
  336.  
  337.     DisposeDocumentData(@gDocument);
  338.  
  339.     {    Close our connection to the QuickDraw 3D library}
  340.     myStatus := Q3Exit;
  341.     if (myStatus = kQ3Failure) then
  342.         DebugStr('ErExit returned failure.');
  343.  
  344. end.
  345.